// JavaScript Document
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(obj, fromIndex) {
		if (fromIndex == null) {
			fromIndex = 0;
		} else if (fromIndex < 0) {
			fromIndex = Math.max(0, this.length + fromIndex);
		}
		for (var i = fromIndex, j = this.length; i < j; i++) {
			if (this[i] === obj)
				return i;
		}
		return -1;
	};
}

var previousPage = "";
var nextPage = "";
var currentPageIndex = -1;
/**
 * Loads a page from the vht_views into the Content item
 */
function loadPage(page) {
	var page = stringRemove("#", page);
	var index = templatePages.indexOf(page);
	$('#task_value').html((index + 1) + "/" + (templatePages.length));
	$('#loading').fadeIn("fast");

	$('#content').fadeOut("fast", function() {
				$("#bcrumbbubble_" + currentPageIndex).removeClass("cpActive");
				$("#bcrumbbubble_" + currentPageIndex).removeClass("cp26");
				$("#bcrumbbubble_" + currentPageIndex).addClass("cp9");

				$("#bcrumbbubble_" + index).removeClass("cp26");
				$("#bcrumbbubble_" + index).removeClass("cp9");
				$("#bcrumbbubble_" + index).addClass("cpActive");

				$("#bcrumb" + index).attr("href", "#" + page);

				// switch current page to the one we're setting it to
				currentPageIndex = index;

				var myData = new Object;
				var url_string = 'views/' + page + '.php';

				// if this is not one of the named hard-coded pages, it is a db
				// numbered
				// page
				if (!isNaN(page)) {
					// is a db numbered page
					myData.page_id = page;
					url_string = 'views/pageWriter.php';
				}

				$.ajax({
							url : url_string,
							type : 'GET',
							dataType : 'html',
							data : myData,
							success : function(html) {
								$('#content').html(html);
								$('#loading').fadeOut("fast", function() {
											$('#content').fadeIn();
										});

								window.onload();
								pageLoaded();
							}
						});
			});
}

/**
 * Reloads the current page from the vht_views into the Content item
 */
function reloadPage(page) {
	$('#loading').fadeIn("fast");
	var page = stringRemove("#", page);

	var myData = new Object;
	var url_string = 'views/' + page + '.php';

	// if this is not one of the named hard-coded pages, it is a db
	// numbered
	// page
	if (!isNaN(page)) {
		// is a db numbered page
		myData.page_id = page;
		url_string = 'views/pageWriter.php';
	}

	$.ajax({
				url : url_string,
				type : 'GET',
				dataType : 'html',
				data : myData,
				success : function(html) {
					$('#content').html(html);
					$('#loading').fadeOut("fast");

					window.onload();
					pageLoaded();
				}
			});
}

/**
 * Gets the page before the given page
 * 
 * @param page
 * @return string The previous page or null
 */
function getPreviousPage(page) {
	var index = templatePages.indexOf(page);
	if (index != -1 && index != 0) {
		return templatePages[index - 1];
	}
	return null;
}

/**
 * Gets the page after the given page
 * 
 * @param page
 * @return string The next page or null
 */
function getNextPage(page) {
	var index = templatePages.indexOf(page);
	if (index != -1 && index != templatePages.length - 1) {
		return templatePages[index + 1];
	}
	return null;
}

/**
 * Sends us to the next page using the hash
 */
function goToNextPage() {
	location.hash = getNextVPPage(templatePages[currentPageIndex]);
}

/**
 * Sends us to the previous page using the hash
 */
function goToPreviousPage() {
	location.hash = getPreviousVPPage(templatePages[currentPageIndex]);
}

/**
 * Removes needle from haystack and returns
 * 
 * @param needle
 *            string to remove
 * @param haystack
 *            string from which to remove
 * @return string haystack without needle
 */
function stringRemove(needle, haystack) {
	var myString;
	var atpos = haystack.indexOf(needle);
	if (atpos > -1) {
		myString = haystack.substring(0, atpos)
				+ haystack.substring(atpos + 1, haystack.length);
	}
	return myString;
}

var current_hash = window.location.hash;

function check_hash() {
	if (window.location.hash != current_hash) {
		current_hash = window.location.hash;
		loadPage(current_hash);
	}
}

/**
 * Reloads the current page
 */
function refreshPage() {
	reloadPage(current_hash);
}

/**
 * Called when login submit button is clicked
 */
function login() {
	$("#content").fadeOut("fast", function() {
				$("#message").html("Checking credentials");
				$("#message").fadeIn("fast");

				var myData = new Object;
				var url_string = 'controllers/check_login.php';
				myData.username = $("#username").val();
				myData.password = $("#password").val();

				$.ajax({
							url : url_string,
							type : 'GET',
							dataType : 'html',
							data : myData,
							success : function(result) {
								if (result == 'true') {
									location = "index.php";
								} else {
									$("#message").html("Login failed");
									$("#content").fadeIn("fast");
								}
							}
						});
			});
}

function updateSuccessful(result) {
	if (result == false) {
		alert("Update failed. Your page is out of sync with the database. REFRESHING PAGE.");
		location.reload(true);
	}
}

$(document).ready(function() {
			// set breadcrumbs to default
			$(".breadcrumb").addClass("cp9");
			$(".breadcrumb").addClass("ui-corner-all");

			if (!location.hash) {
				location.hash = "#login";
			} else {
				refreshPage();
			}

			hashCheck = setInterval("check_hash()", 50);
		});